package com.ylc.chat.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ylc.chat.util.JDBCUtil;
import com.ylc.chat.entity.User;

/**
 * @Author: ylc
 * @Description:用户表持久层
 */
public class UserDao {

    /**
     * 填充User字段
     * @param user
     * @param rs
     */
    private void setUser(User user, ResultSet rs) throws SQLException{
        user.setUid(rs.getInt("uid"));
        user.setUname(rs.getString("uname"));
        user.setUpassword(rs.getString("upassword"));
        user.setOnline(rs.getInt("online"));
    }
	/**
	 * 根据uname获取User对象
	 * @param uname
	 * @return
	 */
	public User getUser(String uname) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        User user = new User();
        try {
            conn = JDBCUtil.getConnection();
            String sql = "select * from user where uname = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,uname);

            rs = pstmt.executeQuery();
            while(rs.next()) {
            	setUser(user,rs);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(rs,pstmt,conn);
        }
        return user;
	}

    /**
     * 根据uid获取用户信息
     * @param uid
     * @return
     */
    public User getUser(int uid) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        User user = new User();
        try {
            conn = JDBCUtil.getConnection();
            String sql = "select * from user where uid = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,uid);

            rs = pstmt.executeQuery();
            while(rs.next()) {
                setUser(user,rs);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(rs,pstmt,conn);
        }
        return user;
    }
	/**
	 * 通过用户名密码获取用户信息
	 * @param uname
	 * @param upassword
	 * @return
	 */
	public User login(String uname, String upassword) {
        Connection conn = null;
        PreparedStatement pstmt =  null;
        ResultSet rs = null;
        User user = new User();
        try {
            conn =  JDBCUtil.getConnection();
            String sql = "select * from user where uname = ? and upassword = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            pstmt.setString(2,upassword);
            rs = pstmt.executeQuery();
           while(rs.next()){
               setUser(user,rs);
           }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs,pstmt,conn);
        }
        return user;
	}

    /**
     * 新增用户
     * @param uname
     * @param upassword
     * @return
     */
    public int addUser(String uname, String upassword) {
        Connection conn = null;
        PreparedStatement pstmt =  null;
        int res = 0;
        try {
            conn =  JDBCUtil.getConnection();
            String sql = "INSERT INTO `user`(uname, upassword, online) VALUES(?, ?, 0)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            pstmt.setString(2,upassword);
            res = pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(pstmt,conn);
        }
        return res;
    }

    /**
     * 判断用户是否已经存在
     * @param uname
     * @return
     */
    public int isSameName(String uname){
        Connection conn = null;
        PreparedStatement pstmt =  null;
        ResultSet rs = null;
        int res = 0;
        try {
            conn =  JDBCUtil.getConnection();
            String sql = "select count(1) from user where uname = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            rs = pstmt.executeQuery();
            while(rs.next()){
                res = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs, pstmt,conn);
        }
        return res;
    }

    /**
     * 修改在线状态
     * @param uname
     * @return
     */
    public int updateOnline(String uname, Integer online) {
        Connection conn = null;
        PreparedStatement pstmt =  null;
        int res = 0;
        try {
            conn =  JDBCUtil.getConnection();
            String sql = "UPDATE `user` SET online=? WHERE uname=?;";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,online);
            pstmt.setString(2,uname);
            res = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(pstmt,conn);
        }
        return res;
    }

    /**
     * 根据用户Id获得用户名
     * @param uid
     * @return
     */
    public String getNameById(int uid){
        Connection conn = null;
        PreparedStatement pstmt =  null;
        ResultSet rs = null;
        String res = "";
        try {
            conn =  JDBCUtil.getConnection();
            String sql = "select uname from user where uid = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,uid);
            rs = pstmt.executeQuery();
            while(rs.next()){
                res = rs.getString("uname");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs,pstmt,conn);
        }
        return res;
    }
}
